%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Parser.tab.h"
%}

NEWLINE         (\r|\n|\r\n)
IDEN            ([^\r\n \t\[\]\.\(\)\{\}\\\/\;\>\<\=,"'`]+)
WHITESPACE      ([ \t]+)
INTEGER         ([+-]?[0-9]+)

%%

{NEWLINE}       { }

{WHITESPACE}    { }

{INTEGER}       {
                    yylval = atoi(yytext);
                    return INTEGER;
                }

"[int]"         {
                    return TYPE_INT;
                }

"[char]"        {
                    return TYPE_CHAR;
                }

"[varchar]"     {
                    return TYPE_VARCHAR;
                }

"QUIT"          {
                    return QUIT;
                }

"DROP"          {
                    return DROP;
                }
"TABLE"         {
                    return TABLE;
                }
"DELETE"        {
                    return DELETE;
                }
"FROM"          {
                    return FROM;
                }
"WHERE"         {
                    return WHERE;
                }
"AND"           {
                    return AND;
                }
"OR"            {
                    return OR;
                }
"SELECT"        {
                    return SELECT;
                }
"INSERT"        {
                    return INSERT;
                }
"INTO"          {
                    return INTO;
                }
"VALUES"        {
                    return VALUES;
                }
"UPDATE"        {
                    return UPDATE;
                }
"SET"           {
                    return SET;
                }
"CREATE"        {
                    return CREATE;
                }
"CHAR"          {
                    return CHAR;
                }
"INT"           {
                    return INT;
                }

"NOT"           {
                    return NOT;
                }

"IS"            {
                    return IS;
                }

"NULL"          {
                    return NIL;
                }

"VARCHAR"       {
                    return VARCHAR;
                }

"<>"            {
                    return NOT_EQUAL;
                }

">="            {
                    return GREATER_EQUAL;
                }

"<="            {
                    return LESS_EQUAL;
                }

">"             {
                    return '>';
                }

"<"             {
                    return '<';
                }

"="             {
                    return '=';
                }

"("             {
                    return '(';
                }

")"             {
                    return ')';
                }

";"             {
                    return ';';
                }

","             {
                    return ',';
                }

"."             {
                    return '.';
                }

"*"             {
                    return '*';
                }

"["[^\[\]]+"]"  {
                    int len = strlen(yytext);
                    char* iden = (char*)malloc(len - 1);
                    strncpy(iden, yytext + 1,  len - 2);
                    iden[len - 2] = 0;
                    yylval = (int)iden;
                    return IDEN;
                }

{IDEN}          {
                    int len = strlen(yytext);
                    char* iden = (char *)malloc(len + 1);
                    strcpy(iden, yytext);
                    yylval = (int)iden;
                    return IDEN;
                }

"'"[^\r\n']+"'" {
                    int len = strlen(yytext);
                    char* iden = (char*)malloc(len - 1);
                    strncpy(iden, yytext + 1,  len - 2);
                    iden[len - 2] = 0;
                    yylval = (int)iden;
                    return STRING;
                }

%%

int yywrap()
{
    return 1;
}

